package com.pxx.item.controller;

import com.pxx.item.pojo.TbSpecGroup;
import com.pxx.item.pojo.TbSpecParam;
import com.pxx.item.service.TbSpecGroupService;
import com.pxx.item.service.TbSpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Yoo19
 * @description :
 * @createDate : 2025/6/26 18:38 38
 */
@RestController
@RequestMapping("/item/spec")
public class SpecGroupController {
    @Autowired
    private TbSpecGroupService tbSpecGroupService;
    @Autowired
    private TbSpecParamService tbSpecParamService;

    /**
     * 根据商品分类id查询规格组列表
     * @param cid
     * @return
     */
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<TbSpecGroup>> queryGroupByCid(@PathVariable("cid")Long cid){
        List<TbSpecGroup> list = tbSpecGroupService.queryGroupByCid(cid);
        if(list == null || list.size() < 1){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @GetMapping("/params")
    public ResponseEntity<List<TbSpecParam>> queryParams(
            @RequestParam(value = "gid",required = false)Long gid,
            @RequestParam(value = "cid",required = false)Long cid,
            @RequestParam(value = "generic",required = false)Boolean generic,
            @RequestParam(value = "searching",required = false)Long searching
                                                         ){
        List<TbSpecParam> list = tbSpecParamService.queryParams(gid,cid,generic,searching);
        if(list == null || list.size() < 1){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);

    }

    @PostMapping("/group")
    public ResponseEntity<Void> addGroup(@RequestBody TbSpecGroup tbSpecGroup){
        tbSpecGroupService.addGroup(tbSpecGroup);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody TbSpecGroup tbSpecGroup){
        tbSpecGroupService.updateGroup(tbSpecGroup);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable("id")Long id){
        tbSpecGroupService.deleteGroup(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/param")
    public ResponseEntity<Void> queryAddParam(@RequestBody TbSpecParam specParam){
         tbSpecParamService.queryAddParam(specParam);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> queryDeleteParam(@PathVariable("id") Long id){
         tbSpecParamService.queryDeleteParam(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PutMapping("/param")
    public ResponseEntity<Void> queryUpdateParam(@RequestBody TbSpecParam specParam){
         tbSpecParamService.queryUpdateParam(specParam);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
